import 'dart:ui';

import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';
import 'package:magic_car/order/order.dart';
import 'package:magic_car/site/editSiteinfo.dart';
import 'package:magic_car/site/sitesinpector.dart';
import 'package:magic_car/user/anserdetail.dart';
import 'package:magic_car/util/HttpHelper.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:toast/toast.dart';

class SiteInfoPage extends StatefulWidget {
  final String sid;
  SiteInfoPage(this.sid);

  @override
  _MyHomePageState createState() => new _MyHomePageState(this.sid);
}

class _MyHomePageState extends State<SiteInfoPage> {
  final String sid;
  _MyHomePageState(this.sid);
  List formList;
  ScrollController _controller = new ScrollController();
  int page = 1;
  bool end = false;
  String keyword = "";
  Map siteInfo = new Map();

  initState() {
    super.initState();
    _controller.addListener(() {
      if (_controller.position.pixels == _controller.position.maxScrollExtent) {
        print('滑动到了最底部');
        loadData();
      }
    });

    formList = [];
    loadData();
  }

  //加载数据
  loadData() async {
    if (end) return;
    Map<String, String> map = new Map();

    map["sid"] = this.sid;

    print(map);

    HttpHelper.post(HttpHelper.API_SiteInfo, (data) async {
      if (data != null) {
        print(data);

        if (data["code"] == "0") {
          Toast.show('无相关信息', context);
        } else {
          setState(() {
            siteInfo = data["info"];
          });
        }
      }
    }, params: map);
  }

  Widget buildImgs(item) {
    List<Widget> tiles = [];

    Widget content;

    List items = item["image_url"];

    for (var i = 0; i < items.length; i++) {
      //List temp1 = items[i] as List;
      tiles.add(Padding(
        padding: new EdgeInsets.fromLTRB(5, 0, 0, 10),
        child: GestureDetector(
            onTap: () {},
            child: Image.network(items[i], width: 100, height: 100)),
      ));
    }
    content = new Wrap(
        children: tiles //重点在这里，因为用编辑器写Column生成的children后面会跟一个<Widget>[]，
        //此时如果我们直接把生成的tiles放在<Widget>[]中是会报一个类型不匹配的错误，把<Widget>[]删了就可以了
        );
    return content;
  }

  Widget buildcontent() {
    List<Widget> tiles = [];
    Widget content;

    if (siteInfo.length > 0) {
      tiles.add(Padding(
          padding: new EdgeInsets.fromLTRB(15, 15, 15, 15),
          child: new Container(
              width: double.infinity - 40,
              decoration: new BoxDecoration(
                color: Colors.white,
              ),
              child: new Column(
                children: <Widget>[
                  Padding(
                      padding: new EdgeInsets.fromLTRB(15, 15, 15, 10),
                      child: Container(
                          height: 100,
                          width: 100,
                          child: ClipRRect(
                            borderRadius: BorderRadius.circular(50),
                            child:siteInfo['avatar']==""?Image.asset("resource/images/user/user6.png"):CachedNetworkImage(imageUrl: siteInfo['avatar'],fit:BoxFit.cover)
                            // child: FadeInImage.assetNetwork(
                            //   placeholder: "resource/images/user/user6.png",
                            //   image: siteInfo['avatar'],
                            //   fit: BoxFit.cover,
                            // ),
                          ))),
                  Padding(
                      padding: new EdgeInsets.fromLTRB(15, 15, 15, 10),
                      child: Text(
                        '名称：' + siteInfo['name'],
                        style: TextStyle(
                            fontSize: 14,
                            color: Color.fromRGBO(120, 120, 120, 1)),
                      )),
                  // Padding(
                  //     padding: new EdgeInsets.fromLTRB(15, 15, 15, 10),
                  //     child: new Image.network(siteInfo['idcardfront'],height: 150),),
                  Padding(
                      padding: new EdgeInsets.fromLTRB(15, 0, 15, 10),
                      child: Text(
                        '区域：' +
                            siteInfo['provinceName'] +
                            siteInfo['cityName'] +
                            siteInfo['areaName'],
                        style: TextStyle(
                            fontSize: 14,
                            color: Color.fromRGBO(120, 120, 120, 1)),
                      )),
                  Padding(
                      padding: new EdgeInsets.fromLTRB(15, 0, 15, 10),
                      child: Text(
                        '详细地址：' + siteInfo['address'],
                        style: TextStyle(
                            fontSize: 14,
                            color: Color.fromRGBO(120, 120, 120, 1)),
                      )),
                  Padding(
                      padding: new EdgeInsets.fromLTRB(15, 0, 15, 10),
                      child: Text(
                        '联系电话：' + siteInfo['mobile'],
                        style: TextStyle(
                            fontSize: 14,
                            color: Color.fromRGBO(120, 120, 120, 1)),
                      )),
                  Padding(
                      padding: new EdgeInsets.fromLTRB(15, 0, 15, 10),
                      child: Text(
                        '添加时间：' + siteInfo['ctime'],
                        style: TextStyle(
                            fontSize: 14,
                            color: Color.fromRGBO(120, 120, 120, 1)),
                      )),
                  Padding(
                      padding: new EdgeInsets.fromLTRB(15, 0, 15, 10),
                      child: Text(
                        siteInfo['is_tingyong'].toString() == "1"
                            ? '状态：停用'
                            : '状态：正常',
                        style: TextStyle(
                            fontSize: 14,
                            color: Color.fromRGBO(120, 120, 120, 1)),
                      )),
                  Padding(
                      padding: new EdgeInsets.fromLTRB(15, 0, 15, 10),
                      child: new Row(
                          crossAxisAlignment: CrossAxisAlignment.center,
                          mainAxisAlignment: MainAxisAlignment.center,
                          children: <Widget>[
                            Padding(
                                padding: new EdgeInsets.fromLTRB(0, 0, 15, 0),
                                child: new GestureDetector(
                                  onTap: () {
                                    //查看站点检测师
                                    Navigator.of(context).push(
                                        new MaterialPageRoute(builder: (_) {
                                      return new SiteInspectorPage(
                                          siteInfo["id"].toString());
                                    }));
                                  },
                                  child: Text(
                                    '检测师 ' + siteInfo['inspector_num'] + '个',
                                    style: TextStyle(
                                        fontSize: 16,
                                        color: Color.fromRGBO(27, 182, 251, 1)),
                                  ),
                                )),
                            Padding(
                                padding: new EdgeInsets.fromLTRB(15, 0, 0, 0),
                                child: new GestureDetector(
                                  onTap: () {
                                    Navigator.of(context).push(
                                        new MaterialPageRoute(builder: (_) {
                                      return new InspectOrderPage(
                                          siteInfo["id"].toString(), '1');
                                    }));
                                  },
                                  child: Text(
                                    '订单数 ' + siteInfo['order_num'] + '单',
                                    style: TextStyle(
                                        fontSize: 16,
                                        color: Color.fromRGBO(27, 182, 251, 1)),
                                  ),
                                )),
                          ])),
                ],
              ))));
    }
    tiles.add(
      Padding(
          padding: new EdgeInsets.fromLTRB(10, 30, 10, 0),
          child: new Container(
            width: double.infinity,
            child: new Card(
              color: Color.fromRGBO(27, 182, 251, 1),
              elevation: 16.0,
              child: new FlatButton(
                child: new Padding(
                  padding: new EdgeInsets.fromLTRB(0, 0, 0, 0),
                  child: new Text(
                    '编辑信息',
                    style: new TextStyle(color: Colors.white, fontSize: 16.0),
                  ),
                ),
                onPressed: () {
                  Navigator.push<Object>(context,
                      new MaterialPageRoute(builder: (BuildContext context) {
                    return EditSitePage(this.sid);
                  })).then((Object result) {
                    loadData();
                  });
                  // Navigator.of(context)
                  //     .push(new MaterialPageRoute(builder: (_) {
                  //   return new EditSitePage(this.sid);
                  // }));
                },
              ),
            ),
          )),
    );

    content = new Column(
        children: tiles //重点在这里，因为用编辑器写Column生成的children后面会跟一个<Widget>[]，
        //此时如果我们直接把生成的tiles放在<Widget>[]中是会报一个类型不匹配的错误，把<Widget>[]删了就可以了
        );
    return content;
  }

  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: new AppBar(
        title: new Text(
          '站点信息',
          style: TextStyle(color: Colors.black),
        ),
        backgroundColor: Colors.white,
        elevation: 0,
         iconTheme: IconThemeData(
          color: Colors.black, //修改颜色
        ),
        // backgroundColor: Color.fromRGBO(255, 204, 48, 1),
      ),
      body: new ListView(children: <Widget>[buildcontent()]),
      //])))),
    );
  }

  Future<Null> _onRefresh() async {
    await Future.delayed(Duration(seconds: 3), () {
      print('refresh');
      setState(() {
        page = 1;
      });
      loadData();
    });
  }

  void deactivate() {}
}
